package com.cheng.controller;


import com.cheng.pojo.*;
import com.cheng.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {


    @Autowired

    private OrderService orderService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private UserService userService;


    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTeamService productTeamService;

    //商家查询订单
    @RequestMapping(value = "/querymerchantsOrder")
    public String toInterviewerCount(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                                     Model model,
                                     HttpSession session) {
        User user = (User) session.getAttribute("user");
        PageInfo<Order> pageInfo = orderService.querymerchantsOrder(page, pageSize,user.getUserCode());
        System.out.println(pageInfo+"1111111111111111111111111");
        model.addAttribute("orders", pageInfo);
        return "orderlist";
    }



    //商家取消订单
    @RequestMapping("/merchantsDeleteOrder")
    public String merchantsDeleteOrder(String ordercode,String userCode,String name,String total_price,String productcode,String adult_number,String child_number){


        usersService.addMoney(name,total_price);

        userService.refund(userCode,total_price);


        orderService.merchantsDeleteOrder(ordercode);

        productService.remove_number(productcode,adult_number,child_number);

        productTeamService.remove_number(productcode,adult_number,child_number);


        return "redirect:querymerchantsOrder";
    }

    //用户取消订单
    @RequestMapping("/usersDeleteOrder")
    public String usersDeleteOrder(String ordercode,String userCode,String name,String total_price,String productcode,String adult_number,String child_number){


        usersService.addMoney(name,total_price);

        userService.refund(userCode,total_price);


        orderService.merchantsDeleteOrder(ordercode);

        productService.remove_number(productcode,adult_number,child_number);

        productTeamService.remove_number(productcode,adult_number,child_number);


        return "redirect:queryusersOrder";
    }


    //用户查询订单
    @RequestMapping(value = "/queryusersOrder")
    public String queryusersOrder(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                                     Model model,
                                     HttpSession session) {
         Users users = (Users) session.getAttribute("users");
        PageInfo<Order> pageInfo = orderService.queryusersOrder(page, pageSize,users.getName());
        System.out.println(pageInfo+"1111111111111111111111111");
        model.addAttribute("orders", pageInfo);
        return "front_end/travel_order";
    }


    //详情
//    @RequestMapping(value = "/querydetail")
//    public String querydetail(int cno,String productcode,Model model){
//
//            List<Product> detail = productService.detail(cno, productcode);
//            System.out.println("1111111111111111111111111111111"+detail);
//
//            model.addAttribute("product",detail);
//            return "front_end/travel_details";
//
//
//
//
//    }


}
